<template>
  <div class="new">
    <van-overlay :show="overlayShow" :z-index="1000">
      <div style="display: flex;align-items: center;justify-content: center;height: 100%;">
        <van-loading type="spinner" color="#1989fa">请稍候...</van-loading>
      </div>
    </van-overlay>
    <div style="margin-bottom: 10vh;">
      <van-collapse v-model="activeNames">
        <van-collapse-item v-for="item in jsondata.head" :key="item.grouptitle" :name="item.grouptitle" :title="item.grouptitle">
          <van-cell-group style="border:0px;">
            <div style="border-bottom:1px solid #ebedf0" v-for="itemdata in item.groupvalue" :key="itemdata.ID">
              <div v-if="itemdata.sUIType == 'datetime'">
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.value" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" v-model="itemdata.value" required disabled clearable :label="itemdata.sTitle" :error-message="itemdata.errormsg" placeholder="请输入" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-field :id="itemdata.sField" v-model="itemdata.value" readonly required clearable :label="itemdata.sTitle" @click="openDatePicker(itemdata)" :error-message="itemdata.errormsg" placeholder="请输入" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" v-model="itemdata.value" disabled clearable :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field :id="itemdata.sField" v-model="itemdata.value" readonly clearable :label="itemdata.sTitle" @click="openDatePicker(itemdata)" :error-message="itemdata.errormsg" placeholder="请输入" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'code'">
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.value" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field :id="itemdata.sField" clearable v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" placeholder="请输入" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'enum' && itemdata.sUITypeExp == 'normal'">
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" readonly required v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnum(itemdata)" :error-message="itemdata.errormsg" placeholder="请选择" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" readonly v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnum(itemdata)" :error-message="itemdata.errormsg" placeholder="请选择" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'enum' && itemdata.sUITypeExp == 'multi'">
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnumMulti(itemdata)" :error-message="itemdata.errormsg" placeholder="请选择" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_enum'" readonly v-model="itemdata.value" :label="itemdata.sTitle" @click="openEnumMulti(itemdata)" :error-message="itemdata.errormsg" placeholder="请选择" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'ref' && itemdata.sUITypeExp == 'normal'">
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" placeholder="请选择" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" placeholder="请选择" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @change="openRef(itemdata, 'headdata', '', 0)" @click-right-icon="openRef(itemdata, 'headdata', '', 0)" @click-input="onClickInput" @click-left-icon="onClickLeftIcon" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" placeholder="请选择" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" placeholder="请选择" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @change="openRef(itemdata, 'headdata', '', 0)" @click-right-icon="openRef(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'ref' && itemdata.sUITypeExp == 'multi'">
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" placeholder="请选择" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" placeholder="请选择" required clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRefMulti(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" placeholder="请选择" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_ref'" placeholder="请选择" clearable v-model="itemdata.value" :label="itemdata.sTitle" right-icon="search" @input="clearRefValue(itemdata)" @clear="clearRefValue(itemdata)" @click-right-icon="openRefMulti(itemdata, 'headdata', '', 0)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'bool'">
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_bool'" placeholder="请输入" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_bool'" placeholder="请输入" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openBool(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_bool'" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.valuehidden" />
                  <van-field :id="itemdata.sField + '_bool'" placeholder="请输入" v-model="itemdata.value" :label="itemdata.sTitle" @click="openBool(itemdata)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'file'">
                <div style="color:#323233;font-size:14px;line-height:24px;padding:5px;">{{ itemdata.sTitle }}</div>
                <input type="hidden" v-model="itemdata.valuehidden" />
                <input type="hidden" v-model="itemdata.value" />
                <van-uploader placeholder="请上传文件" v-model="itemdata.valuelist" :after-read="onFileHead" @delete="delFileHead" accept="*" multiple :name="itemdata.sField">
                </van-uploader>
              </div>
              <div v-else-if="itemdata.sUIType == 'picture'">
                <div style="color:#323233;font-size:14px;line-height:24px;padding:5px;">{{ itemdata.sTitle }}</div>
                <input type="hidden" v-model="itemdata.valuehidden" />
                <input type="hidden" v-model="itemdata.value" />
                <van-uploader placeholder="请上传图片" v-model="itemdata.valuelist" capture="camera" :after-read="onImageHead" @delete="delImageHead" accept="image/*" multiple :name="itemdata.sField">
                </van-uploader>
              </div>
              <div v-else-if="itemdata.sUIType == 'string' && (itemdata.sUITypeExp == 'long' || itemdata.sUITypeExp == 'big')">
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.value" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-field placeholder="请输入" type="textarea" rows="1" autosize :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-field placeholder="请输入" type="textarea" rows="1" autosize :id="itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" @input="checkData(itemdata)" @blur="checkData(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field type="textarea" rows="1" autosize :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field placeholder="请输入" type="textarea" rows="1" autosize :id="itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'string' && itemdata.sUITypeExp == 'normal' && itemdata.sFormat == 'yearmonth;'">
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.value" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-field placeholder="请输入" :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-field placeholder="请输入" :id="itemdata.sField" required v-model="itemdata.value" :label="itemdata.sTitle" @click="openYearMonth(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field placeholder="请输入" :id="itemdata.sField" v-model="itemdata.value" :label="itemdata.sTitle" @click="openYearMonth(itemdata)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'string' && itemdata.sUITypeExp == 'normal' && itemdata.sFormat.indexOf('refer:') >= 0">
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.value" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-field placeholder="请输入" :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-field placeholder="请输入" :id="itemdata.sField" required readonly v-model="itemdata.value" :label="itemdata.sTitle" @click="openRefer(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field placeholder="请输入" :id="itemdata.sField" v-model="itemdata.value" readonly :label="itemdata.sTitle" @click="openRefer(itemdata)" :error-message="itemdata.errormsg" />
                </template>
              </div>
              <div v-else-if="itemdata.sUIType == 'map'">
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.value" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" required disabled :value="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-field placeholder="请选择" :id="itemdata.sField" required :value="itemdata.value" :label="itemdata.sTitle" right-icon="location-o" @click-right-icon="openMap(itemdata)" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" disabled :value="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field placeholder="请选择" :id="itemdata.sField" clearable :value="itemdata.value" :label="itemdata.sTitle" right-icon="location-o" @click-right-icon="openMap(itemdata)" />
                </template>
              </div>
              <div v-else>
                <template v-if="itemdata.lCanOutput === '0'">
                  <input type="hidden" :id="itemdata.sField" v-model="itemdata.value" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1' && itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" required disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lMustInput === '1'">
                  <van-field placeholder="请输入" :id="itemdata.sField" required clearable v-model="itemdata.value" :label="itemdata.sTitle" @input="checkData(itemdata)" @blur="checkData(itemdata)" :error-message="itemdata.errormsg" />
                </template>
                <template v-else-if="itemdata.lCanInput === '0'">
                  <van-field :id="itemdata.sField" disabled v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
                <template v-else>
                  <van-field placeholder="请输入" :id="itemdata.sField" clearable v-model="itemdata.value" :label="itemdata.sTitle" :error-message="itemdata.errormsg" />
                </template>
              </div>
            </div>
          </van-cell-group>
        </van-collapse-item>
      </van-collapse>
    </div>
    <van-popup v-model="datepickershow" round position="bottom">
      <van-datetime-picker v-model="currentDate" type="date" @confirm="selectedDate" />
    </van-popup>
    <van-popup v-model="datetimeshow" round position="bottom">
      <van-datetime-picker v-model="currentDate" type="datetime" @confirm="selectedDateTime" />
    </van-popup>
    <van-popup v-model="yearmonthshow" round position="bottom">
      <van-datetime-picker v-model="currentYearMonth" type="yecar-month" @confirm="selectedYearMonth" />
    </van-popup>
    <van-action-sheet v-model="enumshow" :actions="actions" @select="selectedEnum" />
    <van-popup v-model="enumMultishow" round position="bottom">
      <van-checkbox-group v-model="selecteddata" ref="checkboxGroup">
        <van-cell v-for="(item, index) in enumMulti" :key="item.id" :title="item.name" @click="toggle(index)">
          <van-checkbox :name="item" ref="checkboxes" slot="right-icon" />
        </van-cell>
      </van-checkbox-group>
      <div style="text-align:right;padding-right:10vw;padding-top:2vh;padding-bottom:2vh;">
        <van-button type="info" size="small" @click="selectedEnumMulti">确认</van-button>
      </div>
    </van-popup>
    <van-tabbar v-model="active" safe-area-inset-bottom>
      <van-tabbar-item @click="save">保存</van-tabbar-item>
      <!--<van-tabbar-item @click="saveAndCheckin">保存并签到</van-tabbar-item>-->
      <!-- <van-tabbar-item v-if="isflow" @click="savesubmit">保存并提交</van-tabbar-item> -->

    </van-tabbar>
  </div>
</template>

<script>
import Vue from 'vue'
import { Search, List, Cell, PullRefresh, Toast, Tabbar, TabbarItem, Popup, DatetimePicker, ActionSheet, Tab, Tabs, Collapse, CollapseItem, CheckboxGroup, Checkbox, Uploader, Overlay, Loading, Dialog, NumberKeyboard } from 'vant'
import qs from 'qs'
import wx from 'weixin-js-sdk'
import * as dd from 'dingtalk-jsapi'
import { lazyAMapApiLoaderInstance } from 'vue-amap'
import YYEsnBridge from '@/assets/js/esn.min.js'
import useInput from '@/utils/useinput'
import { fieldMapMixins } from '@/mixins/fieldmap.js'

Vue.use(Search).use(List).use(Cell).use(PullRefresh).use(Toast).use(Tabbar).use(TabbarItem).use(Popup).use(DatetimePicker).use(ActionSheet).use(Tab).use(Tabs).use(Collapse).use(CollapseItem).use(CheckboxGroup).use(Checkbox).use(Uploader).use(Overlay).use(Loading).use(Dialog).use(NumberKeyboard).use(useInput)
export default {
  name: 'activitynew',
  mixins: [fieldMapMixins],
  data () {
    return {
      idobject: this.$route.query.idobject,
      idtemplate: this.$route.query.idtemplate,
      checkedDate: this.$route.query.checkedDate,
      src: this.$route.query.src,
      jsondata: {},
      index: 0,
      active: 0,
      activeNames: [''],
      activeBodyNams: [''],
      datepickershow: false,
      currentDate: new Date(),
      yearmonthshow: false,
      datetimeshow: false,
      currentYearMonth: new Date(),
      enumshow: false,
      actions: [],
      clickmodel: {},
      isLoad: false,
      conditionValHidden: '',
      clickfrom: '',
      clickindexName: '',
      clickindex: 0,
      selecteddata: [],
      enumMultishow: false,
      enumMulti: [],
      isDisable: false,
      overlayShow: false,
      saveBs: 'new',
      buttonName1: '抵达',
      buttonName2: '离开',
      isflow: false,
      submittedByButton: false,
      withCheckin: false
    }
  },
  mounted: function () {
    //console.log('start created')
    if (!this.isLoad) {
      let data = {
        idtemplate: this.idtemplate,
        checkedDate: this.checkedDate
      }
      this.axios({
        method: 'post',
        url: '/api/' + this.idobject + '.new.do',
        data: qs.stringify(data)
      }).then(res => {
        //console.log(res)
        if (res.data.flag === '1') {
          Toast(res.data.des)
          if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
            this.loginout()
          }
        } else {
          this.jsondata = res.data.jsondata
          this.isflow = res.data.isflow
          for (let i = 0; i < res.data.jsondata.head.length; i++) {
            this.activeNames.push(res.data.jsondata.head[i].grouptitle)
          }
          //console.log('1111jsondata:' + JSON.stringify(this.jsondata))

          for (let groupindex in this.jsondata.head) {
            let itemgroup = this.jsondata.head[groupindex].groupvalue
            for (let itemindex in itemgroup) {
              let itemdata = itemgroup[itemindex]
              if (!this.checkedDate) {
                let tmpd = new Date()
                this.checkedDate = tmpd.getFullYear() + '-' + this.add0(tmpd.getMonth() + 1) + '-' + this.add0(tmpd.getDate())
              }

              if (itemdata['sField'] === 'date') {
                let year = parseInt(this.checkedDate.split('-')[0])
                let month = parseInt(this.checkedDate.split('-')[1])
                let day = parseInt(this.checkedDate.split('-')[2])

                itemdata['value'] = year + '-' + this.add0(month) + '-' + this.add0(day)
              }
              if (itemdata['sField'] === 'kssj') {
                let d = new Date()
                let year = parseInt(this.checkedDate.split('-')[0])
                let month = parseInt(this.checkedDate.split('-')[1]) - 1
                let day = parseInt(this.checkedDate.split('-')[2])

                d.setFullYear(year)
                d.setMonth(parseInt(month))
                d.setDate(day)

                let hour = d.getHours()
                let mins = d.getMinutes()
                let secs = d.getSeconds()

                if (mins > 0) {
                  hour += 1
                }
                hour = this.add0(hour)
                mins = this.add0(0)
                secs = this.add0(0)

                d.setHours(hour)
                d.setMinutes(mins)
                d.setSeconds(secs)

                itemdata['value'] = this.concatTime(d)
              }

              if (itemdata['sField'] === 'jssj') {
                let d = new Date()
                let year = parseInt(this.checkedDate.split('-')[0])
                let month = parseInt(this.checkedDate.split('-')[1]) - 1
                let day = parseInt(this.checkedDate.split('-')[2])

                d.setFullYear(year)
                d.setMonth(parseInt(month))
                d.setDate(day)

                let hour = d.getHours()
                let mins = d.getMinutes()
                let secs = d.getSeconds()

                hour += 2
                mins = this.add0(0)
                secs = this.add0(0)
                d.setHours(hour)
                d.setMinutes(mins)
                d.setSeconds(secs)

                itemdata['value'] = this.concatTime(d)
              }
            }
          }
          this.isLoad = true
        }
      }).catch(res => {
        this.reserror(res)
      })
    }
  },
  methods: {
    // 保证不出现个位数情况
    add0 (m) { return m < 10 ? '0' + m : m },
    onClickInput () {
      // 禁用点击输入框的事件
    },
    onClickLeftIcon () {
      // 删除内容
      if (this.value) {
        this.value = ''
      }
    },
    // 时间戳转化成时间格式
    timeFormat (timestamp) {
      let time = new Date(timestamp)
      return this.concatTime(time)
    },
    concatTime (time) {
      let year = time.getFullYear()
      let month = time.getMonth() + 1
      let date = time.getDate()
      let hours = time.getHours()
      let minutes = time.getMinutes()
      let seconds = time.getSeconds()
      return year + '-' + this.add0(month) + '-' + this.add0(date) + ' ' + this.add0(hours) + ':' + this.add0(minutes) + ':' + this.add0(seconds)
    },

    Add1DateStr (dd) {
      dd.setDate(dd.getDate() + 1)
      return this.concatTime(dd)
    },
    save () {
      if (!this.isDisable) {
        let cansave = 1
        this.isDisable = true
        this.overlayShow = true
        let data = {}

        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
            if (itemdata.lMustInput === '1' && itemdata.value === '') {
              itemdata.errormsg = '不能为空'
              cansave = 0
            } else {

            }
          }
        }
        if (this.withCheckin) {
          data['activity_state'] = '2c9bb2547f9603fd01806396d1f3781b'
          data['activity_state_enum'] = '签到完成'
        } else {
          data['activity_state'] = 'ff8080813c56d95d013c56e20b162326'
          data['activity_state_enum'] = '待拜访'
        }

        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        let sendUrl = '/api/' + this.idobject + '.newsave.do'
        this.axios({
          method: 'post',
          url: sendUrl,
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            if (this.withCheckin) {
              Toast('保存并签到成功')
            } else {
              Toast('保存成功')
            }

            this.submittedByButton = true
            if (this.src === 'list') {
              // this.$router.push({ name: 'list', query: { idobject: this.idobject } })
              this.$router.go(-1)
            } else {
              this.$router.push({ name: 'bizcalendar' })
            }
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          this.isDisable = false
          this.overlayShow = false
          //console.log(res)
        })
      }
    },
    saveTimeAndAddress (bs) {
      let cansave = 1
      this.isDisable = true
      this.overlayShow = true
      let data = {}
      let baddress = ''
      let jd = ''
      let wd = ''
      let distanceItem = {}
      let beginItem = {}
      let endItem = {}
      let _this1 = this
      data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
      data[this.idobject + '_idtemplate'] = this.idtemplate

      // ok

      data['hasstarted'] = this.hasstarted
      data['hasdeparted'] = this.hasdeparted
      for (let groupindex in this.jsondata.head) {
        let itemgroup = this.jsondata.head[groupindex].groupvalue
        for (let itemindex in itemgroup) {
          let itemdata = itemgroup[itemindex]
          if (itemdata.sField === 'baddress') {
            baddress = itemdata.value
            beginItem = itemdata
          }
          if (itemdata.sField === 'eaddress') {
            endItem = itemdata
          }
          if (itemdata.sField === 'idaccount') {
            jd = itemdata.jd
            wd = itemdata.wd
          }
          if (itemdata.sField === 'distance') {
            distanceItem = itemdata
          }
          if (itemdata.sUIType === 'ref') {
            data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
            data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
          } else if (itemdata.sUIType === 'enum') {
            data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
            data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
          } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
            data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
            data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
          } else {
            data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
          }

          if (itemdata.lMustInput === '1' && itemdata.value === '') {
            itemdata.errormsg = '不能为空'
            cansave = 0
          }
        }
      }

      var geocoder = new AMap.Geocoder({
      })

      let lnglat = []
      let objectfield = ''
      if (bs === 'end') {
        objectfield = this.idobject + '_eaddress'
        lnglat = data[this.idobject + '_eaddress'].split(',')
      } else {
        objectfield = this.idobject + '_baddress'
        lnglat = data[this.idobject + '_baddress'].split(',')
      }

      if (jd && wd) {
        lazyAMapApiLoaderInstance.load().then(() => {
          AMap.plugin('AMap.Geocoder', function () {
            var lnglat = [Number(baddress.split(',')[0]), Number(baddress.split(',')[1])]
            var p2 = [Number(jd), Number(wd)]
            var distance = AMap.GeometryUtil.distance(lnglat, p2)
            distanceItem.value = Math.round(distance)
            data['activity_distance'] = distanceItem.value
          })
        })
      }

      geocoder.getAddress(lnglat, function (status, result) {
        var address = ''
        // //console.log('地图status:' + status)
        // //console.log('地图result' + JSON.stringify(result))
        if (status === 'complete' && result.regeocode) {
          address = result.regeocode.formattedAddress
        } else {
          // Toast('根据经纬度获取地址失败')
        }

        data[objectfield] = address + '&' + data[objectfield]
        if (bs === 'end') {
          endItem.value = data[objectfield]
        } else {
          beginItem.value = data[objectfield]
        }

        // _this1.axios({
        //   method: 'post',
        //   url: '/api/activity.begin.do',
        //   data: qs.stringify(data)
        // }).then(res => {
        //   if (res.data.flag === '0') {
        //     data[_this1.idobject + '_id'] = res.data.saveid
        //   } else {
        //     Toast(res.data.des)
        // if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
        //  this.loginout()
        // }
        //   }
        //   //console.log(res)
        // }).catch(res => {
        //   //console.log(res)
        // })

        if (cansave === 0) {
          _this1.isDisable = false
          _this1.overlayShow = false
          return
        }
        // 直接保存
        data['activity_state'] = '2c9bb2547f9603fd01806396d1f3781b'
        data['activity_state_enum'] = '签到完成'

        let sendUrl = '/api/' + _this1.idobject + '.newsave.do'
        _this1.axios({
          method: 'post',
          url: sendUrl,
          data: qs.stringify(data)
        }).then(res => {
          _this1.isDisable = false
          _this1.overlayShow = false
          if (res.data.flag === '0') {
            Toast('保存并签到成功')
            _this1.submittedByButton = true
            if (_this1.src === 'list') {
              _this1.$router.push({ name: 'list', query: { idobject: _this1.idobject } })
            } else {
              _this1.$router.push({ name: 'bizcalendar' })
            }
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          _this1.isDisable = false
          _this1.overlayShow = false
          //console.log(res)
        })
      })
    },
    savesubmit () {
      let cansave = 1
      this.submittedByButton = true
      if (!this.isDisable) {
        this.isDisable = true
        this.overlayShow = true
        let data = {}
        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
            if (itemdata.lMustInput === '1' && itemdata.value === '') {
              itemdata.errormsg = '不能为空'
              cansave = 0
            }
          }
        }
        if (this.jsondata.hasdetail === '1') {
          for (let indexName in this.jsondata.bodydata) {
            for (let i = 0; i < this.jsondata.bodydata[indexName].length; i++) {
              let item = this.jsondata.bodydata[indexName][i]
              for (let itemindex in item) {
                let itemdata = item[itemindex]
                if (itemdata.sUIType === 'ref') {
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
                } else if (itemdata.sUIType === 'enum') {
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
                } else {
                  data[i + '_' + itemdata.idObject + '_' + itemdata.sField] = itemdata.value
                }
                if (itemdata.lMustInput === '1' && itemdata.value === '') {
                  itemdata.errormsg = '不能为空'
                  cansave = 0
                }
              }
            }
          }
        }
        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          return
        }
        this.axios({
          method: 'post',
          url: '/api/' + this.idobject + '.newsavesubmit.do',
          data: qs.stringify(data)
        }).then(res => {
          this.isDisable = false
          this.overlayShow = false
          if (res.data.flag === '0') {
            this.submittedByButton = true
            Toast('保存成功')
            if (this.$route.query.newflag === 'refnew') {
              this.$store.commit('refReBack', { ref_show: 'true' })
              this.$router.go(-1)
              return
            }
            this.$router.push({ name: 'list', query: { idobject: this.idobject } })
          } else {
            Toast(res.data.des)
            if (res.data.des && res.data.des.indexOf('登录过期') >= 0) {
              this.loginout()
            }
          }
          //console.log(res)
        }).catch(res => {
          //console.log(res)
          this.isDisable = false
          this.overlayShow = false
        })
      }
    },
    begin () {
      if (this.buttonName1 === '抵达') {
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sField === 'sname') {
              if (itemdata.value === '') {
                Toast('请输入主题')
                return
              }
            }
          }
        }
        this.getlocation('begin')
        this.saveBs = 'edit'
      }
    },
    end () {
      if (this.buttonName1 === '抵达') {
        Toast('请先进行抵达操作')
        return
      }
      if (this.buttonName2 === '离开') {
        this.getlocation('end')
      }
    },
    getlocation (type1) {
      let ua = window.navigator.userAgent.toLowerCase()
      let logintype = 'wechat'
      let tourl = window.location.href
      let clttype = 'android'
      if (ua.indexOf('iphone') !== -1) {
        clttype = 'ios'
      }
      // if (ua.indexOf('wxwork') !== -1 || ua.indexOf('micromessenger') !== -1) {
      // } else {
      //   logintype = 'dingding'
      //   tourl = window.location.protocol + '//' + window.location.host + '/vue-common/dist/main'
      // }

      if (ua.indexOf('wxwork') !== -1) {
        logintype = 'wxwork'
      } else if (ua.indexOf('dingtalk') !== -1) {
        logintype = 'dingding'
        tourl = window.location.protocol + '//' + window.location.host + '/vue-common/dist/main'
      } else if (ua.indexOf('micromessenger') !== -1) {
        logintype = 'wechat'
      } else if (ua.indexOf('tenantid') !== -1) {
        // 友空间
        logintype = 'ykj'
      } else {
        logintype = 'browser'
      }
      if (logintype === 'browser') {
        alert('请在钉钉或微信端登录!')
        this.$router.go(-1)
      }

      let _this = this
      let data = {
        url: tourl,
        type: logintype,
        clttype: clttype
      }
      let url = '/api/' + this.idobject + '.getlocation.do'
      this.axios({
        method: 'post',
        url: url,
        data: qs.stringify(data)
      }).then(res => {
        if (res.data.flag === '1' && res.data.des && res.data.des.indexOf('登录过期') >= 0) {
          Toast(res.data.des)
          this.loginout()
        } else {
          let data = res.data
          if (logintype === 'wechat' || logintype === 'wxwork') {
            wx.config({
              beta: true,
              debug: false,
              appId: data.appId,
              timestamp: data.timestamp,
              nonceStr: data.nonceStr,
              signature: data.signature,
              jsApiList: [
                'chooseImage',
                'previewImage',
                'uploadImage',
                'downloadImage',
                'previewFile',
                'getLocation'
              ]
            })
            wx.ready(function () {
              wx.getLocation({
                isHighAccuracy: true,
                type: 'gcj02', // 默认为wgs84的gps坐标，如果要返回直接给openLocation用的火星坐标，可传入'gcj02'

                success: function (res) {
                  let latitude = res.latitude // 纬度，浮点数，范围为90 ~ -90
                  let longitude = res.longitude // 经度，浮点数，范围为180 ~ -180。
                  for (let groupindex in _this.jsondata.head) {
                    for (let itemindex in _this.jsondata.head[groupindex].groupvalue) {
                      let item = _this.jsondata.head[groupindex].groupvalue[itemindex]
                      if (type1 === 'begin') {
                        if (item.sField === 'baddress') {
                          item.value = longitude + ',' + latitude
                        }
                        if (item.sField === 'dbegin') {
                          let _currenttime = _this.currenttime()
                          item.value = _currenttime
                          _this.buttonName1 = _currenttime.split(' ')[1].substring(0, 5)
                        }
                      }
                    }
                  }

                  _this.saveTimeAndAddress(type1)
                },
                fail: function (err) {
                  alert('请检查定位是否已经打开!')
                  //console.log(JSON.stringify(err))
                }
              })
            })
          } else if (logintype === 'dingding') {
            dd.config({
              agentId: data.agentid,
              corpId: data.appId,
              timeStamp: data.timestamp,
              nonceStr: data.nonceStr,
              signature: data.signature,
              type: 0,
              jsApiList: [
                'runtime.info',
                'biz.contact.choose',
                'device.notification.confirm',
                'device.notification.alert',
                'device.notification.prompt',
                'biz.ding.post',
                'biz.util.openLink',
                'device.geolocation.get'
              ]
            })
            dd.error(function (error) {
              alert('dd error: ' + JSON.stringify(error))
            })
            dd.ready(function () {
              dd.device.geolocation.get({
                targetAccuracy: 200,
                coordinate: 1,
                withReGeocode: false,
                useCache: true,
                onSuccess: function (result) {
                  let longitude = result.longitude
                  let latitude = result.latitude
                  for (let groupindex in _this.jsondata.head) {
                    for (let itemindex in _this.jsondata.head[groupindex].groupvalue) {
                      let item = _this.jsondata.head[groupindex].groupvalue[itemindex]

                      if (type1 === 'begin') {
                        if (item.sField === 'baddress') {
                          item.value = longitude + ',' + latitude
                        }
                        if (item.sField === 'dbegin') {
                          let _currenttime = _this.currenttime()
                          item.value = _currenttime
                          _this.buttonName1 = _currenttime.split(' ')[1].substring(0, 5)
                        }
                      } else if (type1 === 'end') {
                        if (item.sField === 'dend') {
                          let _currenttime = _this.currenttime()
                          item.value = _currenttime
                          _this.buttonName2 = _currenttime.split(' ')[1].substring(0, 5)
                        }
                        if (item.sField === 'eaddress') {
                          item.value = longitude + ',' + latitude
                        }
                      }
                    }
                  }
                  _this.saveTimeAndAddress(type1)
                },
                onfail: function (err) {
                  alert('请检查定位是否已经打开!')
                  //console.log(JSON.stringify(err))
                }
              })
            })
          } else if (logintype === 'ykj') {
            YYEsnBridge.ready(function () {
              YYEsnBridge.do('getLocationInfo', {
                agentId: data.agentid,
                timeStamp: data.timestamp,
                signature: data.signature,
                'success': function (res) {
                  let longitude = res.longitude
                  let latitude = res.latitude
                  for (let groupindex in _this.jsondata.head) {
                    for (let itemindex in _this.jsondata.head[groupindex].groupvalue) {
                      let item = _this.jsondata.head[groupindex].groupvalue[itemindex]

                      if (type1 === 'begin') {
                        if (item.sField === 'baddress') {
                          item.value = longitude + ',' + latitude
                        }
                        if (item.sField === 'dbegin') {
                          let _currenttime = _this.currenttime()
                          item.value = _currenttime
                          _this.buttonName1 = _currenttime.split(' ')[1].substring(0, 5)
                        }
                      } else if (type1 === 'end') {
                        if (item.sField === 'dend') {
                          let _currenttime = _this.currenttime()
                          item.value = _currenttime
                          _this.buttonName2 = _currenttime.split(' ')[1].substring(0, 5)
                        }
                        if (item.sField === 'eaddress') {
                          item.value = longitude + ',' + latitude
                        }
                      }
                    }
                  }
                  _this.saveTimeAndAddress(type1)
                },
                'fail': function (err) {
                  alert('调用失败,请检查定位是否已经打开!' + JSON.stringify(err))
                  //console.log(JSON.stringify(err))
                },
                'complete': function (res) {
                  // alert('调用完成后执行');
                  //console.log('调用完毕:' + JSON.stringify(res))
                }
              })
            })
          }
        }
      }).catch(function (error) {
        alert(error)
      })
    },
    change (t) {
      if (t < 10) {
        return '0' + t
      } else {
        return t
      }
    },
    currenttime () {
      let d = new Date()
      let year = d.getFullYear()
      let month = this.change(d.getMonth() + 1)
      let day = this.change(d.getDate())
      let hour = this.change(d.getHours())
      let minute = this.change(d.getMinutes())
      let second = this.change(d.getSeconds())
      let time = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':' + second
      return time
    },
    addRow (indexName) {
      let bodycopy = JSON.parse(JSON.stringify(this.jsondata.body[indexName]))
      this.jsondata.bodydata[indexName].push(bodycopy)
    },
    delRow (indexName, index) {
      if (this.jsondata.bodydata[indexName].length === 1) {
        Toast('至少保留一条表体行，不能删除！')
        return
      }
      this.jsondata.bodydata[indexName].splice(index, 1)
    },
    checkData (datamodel) {
      if (datamodel.value === '') {
        datamodel.errormsg = '不能为空'
      } else {
        datamodel.errormsg = ''
      }
    },
    openDatePicker (datamodel) {
      if (datamodel.value != null && datamodel.value !== undefined && datamodel.value !== '') {
        if (datamodel.sUITypeExp !== 'long') {
          datamodel.value = datamodel.value.substring(0, 10)
        }
        let newDate = new Date(datamodel.value.replace(/-/g, '/'))
        this.currentDate = new Date(newDate)
      }
      this.clickmodel = datamodel
      if (datamodel.sUITypeExp === 'long') {
        this.datetimeshow = true
      } else {
        this.datepickershow = true
      }
    },
    selectedDate () {
      //console.log(this.currentDate)
      let year = this.currentDate.getFullYear() // 获取完整的年份(4位,1970-????)
      let month = this.currentDate.getMonth() + 1 // 获取当前月份(0-11,0代表1月)
      let day = this.currentDate.getDate() // 获取当前日(1-31)
      if (month < 10) {
        month = '0' + month
      }
      if (day < 10) {
        day = '0' + day
      }
      let dateString = year + '-' + month + '-' + day
      this.clickmodel.value = dateString
      this.clickmodel.errormsg = ''
      this.datepickershow = false
    },
    selectedDateTime () {
      //console.log(this.currentDate)
      let year = this.currentDate.getFullYear() // 获取完整的年份(4位,1970-????)
      let month = this.currentDate.getMonth() + 1 // 获取当前月份(0-11,0代表1月)
      let day = this.currentDate.getDate() // 获取当前日(1-31)
      let hour = this.currentDate.getHours()
      let minute = this.currentDate.getMinutes()
      if (month < 10) {
        month = '0' + month
      }
      if (day < 10) {
        day = '0' + day
      }
      if (hour < 10) {
        hour = '0' + hour
      }
      if (minute < 10) {
        minute = '0' + minute
      }
      let dateString = year + '-' + month + '-' + day + ' ' + hour + ':' + minute + ':00'
      this.clickmodel.value = dateString
      this.clickmodel.errormsg = ''
      this.datetimeshow = false
    },
    openYearMonth (datamodel) {
      if (datamodel.value != null && datamodel.value !== undefined && datamodel.value !== '') {
        datamodel.value = datamodel.value.substring(0, 7)
        this.currentYearMonth = new Date(datamodel.value)
      }
      this.clickmodel = datamodel
      this.yearmonthshow = true
    },
    selectedYearMonth () {
      //console.log(this.currentYearMonth)
      let year = this.currentYearMonth.getFullYear()
      let month = this.currentYearMonth.getMonth() + 1
      if (month < 10) {
        month = '0' + month
      }
      let datestring = year + '-' + month
      this.clickmodel.value = datestring
      this.clickmodel.errormsg = ''
      this.yearmonthshow = false
    },
    openEnum (datamodel) {
      this.actions = []
      this.clickmodel = datamodel
      let enumid = datamodel.allenumid
      let arrEnumid = enumid.split(';')
      let enumval = datamodel.allenumval
      let arrEnumval = enumval.split(';')
      for (let i = 0; i < arrEnumid.length; i++) {
        this.actions.push({ id: arrEnumid[i], name: arrEnumval[i] })
      }
      this.enumshow = true
    },
    selectedEnum (item) {
      this.clickmodel.valuehidden = item.id
      this.clickmodel.value = item.name
      this.clickmodel.errormsg = ''
      this.enumshow = false
    },
    openEnumMulti (datamodel) {
      this.enumMulti = []
      this.selecteddata = []
      this.clickmodel = datamodel
      let enumid = datamodel.allenumid
      let arrEnumid = enumid.split(';')
      let enumval = datamodel.allenumval
      let arrEnumval = enumval.split(';')
      for (let i = 0; i < arrEnumid.length; i++) {
        if (arrEnumid[i] === '') {
          continue
        }
        this.enumMulti.push({ id: arrEnumid[i], name: arrEnumval[i] })
      }
      this.enumMultishow = true
    },
    toggle (index) {
      this.$refs.checkboxes[index].toggle()
    },
    selectedEnumMulti () {
      let enumid = ''
      let enumval = ''
      this.clickmodel.valuehidden = ''
      this.clickmodel.value = ''
      for (let i = 0; i < this.selecteddata.length; i++) {
        enumid += this.selecteddata[i].id + ';'
        enumval += this.selecteddata[i].name + ';'
      }
      this.clickmodel.valuehidden = enumid
      this.clickmodel.value = enumval
      this.clickmodel.errormsg = ''
      this.enumMultishow = false
    },
    openRefer (datamodel) {
      this.actions = []
      this.clickmodel = datamodel
      let referVal = datamodel.sFormat.substring(6, datamodel.sFormat.length - 1)
      let arrRefer = referVal.split(',')
      for (let i = 0; i < arrRefer.length; i++) {
        this.actions.push({ id: arrRefer[i], name: arrRefer[i] })
      }
      this.enumshow = true
    },
    openBool (datamodel) {
      this.actions = []
      this.clickmodel = datamodel
      this.actions.push({ id: '1', name: '是' })
      this.actions.push({ id: '0', name: '否' })
      this.enumshow = true
    },
    openRef (datamodel, datafrom, indexName, index) {
      this.clickmodel = datamodel
      this.clickfrom = datafrom
      this.clickindexName = indexName
      this.clickindex = index
      this.conditionValHidden = ''
      let inview = datamodel.inview
      if (inview !== '') {
        let arrinview = inview.split(';')
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex]
          let inviewdatas = arrinviewdata.split(':')
          if (inviewdatas[1] === 'eq') {
            if (datafrom === 'headdata') {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                  let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                  if (item.sField === inviewdatas[2].split('_')[2]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (datafrom === 'bodydata') {
              for (let itemindex in this.jsondata.bodydata[indexName][index]) {
                let item = this.jsondata.bodydata[indexName][index][itemindex]
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                }
              }
            }
          } else if (inviewdatas[1] === 'in') {
            //console.log('-------------' + datamodel.value)
            this.conditionValHidden += inviewdatas[0] + ':in:' + datamodel.value + ';'
          }
        }
      }
      //console.log('####', datamodel)
      let componentName = 'ref'
      if (datamodel.idJoinObject === 'employee' || datamodel.idJoinObject === 'dep') {
        componentName = 'orgRef'
      }
      this.$router.push({
        // name: 'ref',
        name: componentName,
        params: {
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: '',
          conditionValHidden: this.conditionValHidden,
          classname: datamodel.classname,
          mode: 'single'
        }
      })
    },
    openMap (data) {
      this.$router.push({
        name: 'map',
        params: {
          sval: this.jsondata.head,
          svalmap: data
        }
      })
    },
    openRefMulti (datamodel, datafrom, indexName, index) {
      this.clickmodel = datamodel
      this.clickfrom = datafrom
      this.clickindexName = indexName
      this.clickindex = index
      this.conditionValHidden = ''
      let inview = datamodel.inview
      //console.log('inview', inview)
      if (inview !== '') {
        let arrinview = inview.split(';')
        for (let inviewindex in arrinview) {
          let arrinviewdata = arrinview[inviewindex]
          let inviewdatas = arrinviewdata.split(':')
          if (inviewdatas[1] === 'eq') {
            if (datafrom === 'headdata') {
              for (let groupindex in this.jsondata.head) {
                for (let itemindex in this.jsondata.head[groupindex].groupvalue) {
                  let item = this.jsondata.head[groupindex].groupvalue[itemindex]
                  if (item.sField === inviewdatas[2].split('_')[2]) {
                    this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                  }
                }
              }
            } else if (datafrom === 'bodydata') {
              for (let itemindex in this.jsondata.bodydata[indexName][index]) {
                let item = this.jsondata.bodydata[indexName][index][itemindex]
                if (item.sField === inviewdatas[0]) {
                  this.conditionValHidden += inviewdatas[0] + ':eqref:' + item.valuehidden + ';'
                }
              }
            }
          } else if (inviewdatas[1] === 'in') {
            this.conditionValHidden += inviewdatas[0] + ':in:;'
          }
        }
      }
      //console.log('conditionValHidden', this.conditionValHidden)

      let componentName = 'ref'
      if (datamodel.idJoinObject === 'employee' || datamodel.idJoinObject === 'dep') {
        componentName = 'orgRef'
      }

      this.$router.push({
        name: componentName,
        // name: 'refmulti',
        params: {
          bo: datamodel.idJoinObject,
          tobo: datamodel.idObject,
          field: datamodel.sField,
          fieldref: datamodel.sFieldRef,
          viewid: datamodel.sRefKey,
          conditionVal: '',
          conditionValHidden: this.conditionValHidden,
          classname: datamodel.classname,
          mode: 'multiple'
        }
      })
    },
    clearRefValue (datamodel) {
      if (!datamodel.value) {
        return // 如果输入框的值为空，不做任何操作
      }
      datamodel.value = '' // 清空输入框的值
      if (datamodel.value === '') {
        datamodel.valuehidden = ''
      }
    },
    onImageHead (file, detail) {
      // 上传
      let deviceFile = []
      let formData = new FormData()
      if (Array.isArray(file)) {
        deviceFile = file
      } else {
        deviceFile.push(file)
      }
      for (let i = 0; i < deviceFile.length; i++) {
        formData.append('image' + i, deviceFile[i].file, deviceFile[i].name)
      }
      this.axios({
        method: 'post',
        url: '/api/upload.imagedo',
        data: formData,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        if (res.data.flag === '0') {
          let fileInfo = res.data.fileInfo
          for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
            let headData = this.jsondata.head[headIdx].groupvalue
            for (let i = 0; i < headData.length; i++) {
              if (headData[i].sField === detail.name) {
                headData[i].valuehidden = fileInfo[0].newname + ';' + headData[i].valuehidden
                headData[i].value = fileInfo[0].oldname + ';' + headData[i].value
              }
            }
          }
          Toast('上传成功')
        } else {
          Toast('上传失败')
        }
      }).catch(res => {
        //console.log(res)
      })
      // let headdata = this.jsondata.head[0].groupvalue
      //console.log(file)
      //console.log(detail)
    },
    delImageHead (file, detail) {
      for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
        let headData = this.jsondata.head[headIdx].groupvalue
        for (let i = 0; i < headData.length; i++) {
          if (headData[i].sField === detail.name) {
            let imgVal = headData[i].valuehidden.split(';')
            let imgTitle = headData[i].value.split(';')
            for (let j = 0; j < imgTitle.length; j++) {
              if (imgTitle[j] === file.file.name) {
                imgVal[j] = ''
                imgTitle[j] = ''
              }
            }
            let imgVal1 = ''
            let imgTitle1 = ''
            for (let j = 0; j < imgTitle.length; j++) {
              if (imgTitle[j] !== '') {
                imgVal1 += imgVal[j] + ';'
                imgTitle1 += imgTitle[j] + ';'
              }
            }
            headData[i].valuehidden = imgVal1
            headData[i].value = imgTitle1
          }
        }
      }
      Toast('删除成功')
    },
    onFileHead (file, detail) {
      // 上传
      let deviceFile = []
      let formData = new FormData()
      if (Array.isArray(file)) {
        deviceFile = file
      } else {
        deviceFile.push(file)
      }
      for (let i = 0; i < deviceFile.length; i++) {
        formData.append('file' + i, deviceFile[i].file, deviceFile[i].name)
      }
      this.axios({
        method: 'post',
        url: '/api/upload.filedo',
        data: formData,
        headers: {
          'Content-Type': 'multipart/form-data'
        }
      }).then(res => {
        if (res.data.flag === '0') {
          let fileInfo = res.data.fileInfo
          for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
            let headData = this.jsondata.head[headIdx].groupvalue
            for (let i = 0; i < headData.length; i++) {
              if (headData[i].sField === detail.name) {
                headData[i].valuehidden = fileInfo[0].newname + ';' + headData[i].valuehidden
                headData[i].value = fileInfo[0].oldname + ';' + headData[i].value
              }
            }
          }
          Toast('上传成功')
        } else {
          Toast('上传失败')
        }
      }).catch(res => {
        //console.log(res)
      })
      // let headdata = this.jsondata.head[0].groupvalue
      //console.log(file)
      //console.log(detail)
    },
    delFileHead (file, detail) {
      for (let headIdx = 0; headIdx < this.jsondata.head.length; headIdx++) {
        let headData = this.jsondata.head[headIdx].groupvalue
        for (let i = 0; i < headData.length; i++) {
          if (headData[i].sField === detail.name) {
            let fileVal = headData[i].valuehidden.split(';')
            let fileTitle = headData[i].value.split(';')
            for (let j = 0; j < fileTitle.length; j++) {
              if (fileTitle[j] === file.file.name) {
                fileVal[j] = ''
                fileTitle[j] = ''
              }
            }
            let fileVal1 = ''
            let fileTitle1 = ''
            for (let j = 0; j < fileTitle.length; j++) {
              if (fileTitle[j] !== '') {
                fileVal1 += fileVal[j] + ';'
                fileTitle1 += fileTitle[j] + ';'
              }
            }
            headData[i].valuehidden = fileVal1
            headData[i].value = fileTitle1
          }
        }
      }
      Toast('删除成功')
    },

    saveAndCheckin () {
      if (!this.isDisable) {
        let cansave = 1
        this.isDisable = true
        this.overlayShow = true
        let data = {}

        data[this.idobject + '_id'] = this.jsondata[this.idobject + '_id']
        data[this.idobject + '_idtemplate'] = this.idtemplate
        for (let groupindex in this.jsondata.head) {
          let itemgroup = this.jsondata.head[groupindex].groupvalue
          for (let itemindex in itemgroup) {
            let itemdata = itemgroup[itemindex]
            if (itemdata.sUIType === 'ref') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_ref'] = itemdata.value
            } else if (itemdata.sUIType === 'enum') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_enum'] = itemdata.value
            } else if (itemdata.sUIType === 'file' || itemdata.sUIType === 'picture') {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.valuehidden
              data[itemdata.idObject + '_' + itemdata.sField + '_title'] = itemdata.value
            } else {
              data[itemdata.idObject + '_' + itemdata.sField] = itemdata.value
            }
            if (itemdata.lMustInput === '1' && itemdata.value === '') {
              itemdata.errormsg += itemdata.sField + '不能为空'
              cansave = 0
            } else {

            }
          }
        }

        if (cansave === 0) {
          this.isDisable = false
          this.overlayShow = false
          Toast('请检查必输项')
          //console.log(this.jsondata)
          return
        }
        // 集合了 保存并签到的功能
        this.getlocation('begin')
      }
    }

  },
  // activated () {
  //   if (this.$store.state.ref_show !== '') {
  //     this.clickmodel.value = this.$store.state.ref_show
  //     this.clickmodel.valuehidden = this.$store.state.ref_id
  //     this.clickmodel.errormsg = ''
  //     let fieldmap = this.clickmodel.fieldmap
  //     if (fieldmap !== undefined && fieldmap.length > 0) {
  //       let arrFieldmap = fieldmap.split(';')
  //       let item = this.$store.state.ref_item
  //       if (this.clickfrom === 'headdata') {
  //         for (let groupindex = 0; groupindex < this.jsondata.head.length; groupindex++) {
  //           let itemgroup = this.jsondata.head[groupindex]
  //           for (let itemindex = 0; itemindex < itemgroup.groupvalue.length; itemindex++) {
  //             let itemdata = itemgroup.groupvalue[itemindex]
  //             for (let fieldmapindex in arrFieldmap) {
  //               let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
  //               if (this.$store.state.ref_field === arrFieldMapKey[1]) {
  //                 continue
  //               }
  //               if (itemdata.sField === arrFieldMapKey[1]) {
  //                 if (itemdata.sUIType === 'ref') {
  //                   itemdata.value = item[arrFieldMapKey[0] + '_ref']
  //                   itemdata.valuehidden = item[arrFieldMapKey[0]]
  //                 } else if (itemdata.sUIType === 'enum') {
  //                   itemdata.value = item[arrFieldMapKey[0] + '_enum']
  //                   itemdata.valuehidden = item[arrFieldMapKey[0]]
  //                 } else {
  //                   itemdata.value = item[arrFieldMapKey[0]]
  //                 }
  //               }
  //             }
  //           }
  //         }
  //       } else if (this.clickfrom === 'bodydata') {
  //         for (let itemindex in this.jsondata.bodydata[this.clickindexName][this.clickindex]) {
  //           let itemdata = this.jsondata.bodydata[this.clickindexName][this.clickindex][itemindex]
  //           for (let fieldmapindex in arrFieldmap) {
  //             let arrFieldMapKey = arrFieldmap[fieldmapindex].split(':')
  //             if (this.$store.state.ref_field === arrFieldMapKey[1]) {
  //               continue
  //             }
  //             if (itemdata.sField === arrFieldMapKey[1]) {
  //               if (itemdata.sUIType === 'ref') {
  //                 itemdata.value = item[arrFieldMapKey[0] + '_ref']
  //                 itemdata.valuehidden = item[arrFieldMapKey[0]]
  //               } else if (itemdata.sUIType === 'enum') {
  //                 itemdata.value = item[arrFieldMapKey[0] + '_enum']
  //                 itemdata.valuehidden = item[arrFieldMapKey[0]]
  //               } else {
  //                 itemdata.value = item[arrFieldMapKey[0]]
  //               }
  //             }
  //           }
  //         }
  //       }
  //     }
  //     this.clickmodel = {}
  //     this.clickindex = 0
  //     this.clickindexName = ''
  //     this.clickfrom = ''
  //     this.$store.commit('clearRef')
  //   }
  // },

  beforeRouteLeave (to, from, next) {
    //console.log('to.name:' + to.name)
    //console.log('from.name' + from.name)
    // 点击按钮
    if (this.submittedByButton) {
      next()
      return
    }
    if (to.name === 'list') {
      const v = confirm('当前数据未保存,确定退出吗?')
      if (!v) {
        next(false)
      } else {
        next()
      }
    } else {
      next()
    }
  },
  onInput (value) {
    Toast(value)
  },
  onDelete () {
    Toast('删除')
  }

}
</script>

<style>
.fieldgroup_title {
  text-align: left;
  margin: 0px;
  padding: 25px 15px 15px;
  color: rgba(69, 90, 100, 0.6);
  font-weight: normal;
  font-size: 14px;
}
</style>
